/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsserver;

import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Do Viet Trung
 */
public class AccountXML extends  DefaultHandler
{
    private boolean id,host=false;
    private AccountInfo account;
    private ArrayList<AccountInfo> listAccount = new ArrayList<AccountInfo>();

    public ArrayList<AccountInfo> getListAccount() {
        return listAccount;
    }


//    public void setDocumentLocator(Locator lctr) {
//    }
//
//    public void startDocument() throws SAXException {
//    }
//
//    public void endDocument() throws SAXException {
//    }
//
//    public void startPrefixMapping(String string, String string1) throws SAXException {
//    }
//
//    public void endPrefixMapping(String string) throws SAXException {
//    }

    @Override
    public void startElement(String uri, String localname, String qname, Attributes atrbts) throws SAXException
    {
        if(qname.equals("ID"))
        {
            id = true;
            account = new AccountInfo();
            String username = atrbts.getValue("username");
            String password = atrbts.getValue("passwd");
            account.setPassword(password);
            account.setUsername(username);
        }
        if(qname.equals("Host"))
        {
            host = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException
    {
        if(qName.equals("ID"))
        {
            id = false;
            listAccount.add(account);
        }
        if(qName.equals("Host"))
        {
            host = false;
        }
    }

    @Override
    public void characters(char[] chars, int start, int length) throws SAXException
    {
        String str = new String(chars,start,length);
        if(host)
        {
            account.addAllowDomain(str);
        }
    }

//    @Override
//    public void ignorableWhitespace(char[] chars, int i, int i1) throws SAXException {
//    }
//
//    public void processingInstruction(String string, String string1) throws SAXException {
//    }
//
//    public void skippedEntity(String string) throws SAXException {
//    }

}
